/**
 * ziyi
 */
 module ziyi{
    export class Main extends eui.UILayer {
        protected createChildren(): void {
            super.createChildren();
            //注入自定义的素材解析器
            let assetAdapter = new AssetAdapter();
            egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
            egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
            this.runGame().catch(e => {
                console.log(e);
            })
        }
        private async runGame() {
            await this.loadResource()
            this.createGameScene();
            const result = await RES.getResAsync("description_json")
        }
        private async loadResource() {
            try {
                await RES.loadConfig("resource/default.res.json", "resource/");
                await this.loadTheme();
            }
            catch (e) {
                console.error(e);
            }
        }
        private loadTheme() {
            return new Promise((resolve, reject) => {
                // load skin theme configuration file, you can manually modify the file. And replace the default skin.
                //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
                let theme = new eui.Theme("resource/default.thm.json", this.stage);
                theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                    resolve();
                }, this);
            })
        }
        private pots1;
        private pots2;
        private succ;
        private fail;
        private start;
        private pa;
        private p1:Array<Array<eui.Image>>;
        private succbtn:eui.Group;
        private failbtn:eui.Group;
        private pabtn:eui.Group;
        private startbtn;
        private catStayAndWeizhu=new cats();
        private succeccText:eui.Label;
        private seconds:number=0;
        
        private timer:egret.Timer=new egret.Timer(100,0);
        private cat:egret.MovieClip;//正常猫
        private weizhucat:egret.MovieClip;//被围住的猫
        private row:number=4;//猫所在的行
        private col:number=4;//猫所在的列
        private fp=new findPath();
        //未被围住的猫的movieclip
        private loadRes():void{
            var bg = new eui.Component();
            bg.skinName = "../resource/eui_skins/backgroundSkin.exml";
            this.addChildAt(bg,1);
            // pot.skinName = "../resource/eui_skins/pot1Skin.exml";
            
            this.pots2=new pot2();
            this.succ=new success();
            this.fail=new failed();
            this.start=new startbtn();
            this.pa=new playagain();
            
            this.addChildAt(this.pots2,1);
            this.addChildAt(this.succ,3);
            this.addChildAt(this.fail,3);
            this.addChildAt(this.start,3);
            this.addChildAt(this.pa,3);
            setTimeout(() => {
                this.pots1 = new pot1();
                this.addChildAt(this.pots1,2);
            }, 10);
            
        }
        /**
         * 创建场景界面
         */
        protected createGameScene(): void {
            this.loadRes();
            this.loadTime();
            setTimeout(()=>{
                this.p1=this.pots1.getpots1();
                this.startbtn=this.start.getStartBtn();
                this.startbtn.once(egret.TouchEvent.TOUCH_TAP,()=>{
                    this.startbtn.visible=false;
                    this.init();
                },this);
                this.succbtn=this.succ.getSuccess();
                this.succeccText=<eui.Label>this.succbtn.getElementAt(2);
                this.failbtn=this.fail.getFailed();
                this.pabtn=this.pa.getPlayAgain();
                this.pabtn.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
                    this.init();
                    this.seconds=0;
                    this.fp.setflag(false);
                    this.cat.x=this.p1[this.row][this.col].parent.parent.parent.x+this.p1[this.row][this.col].parent.parent.x+this.p1[this.row][this.col].parent.x+this.p1[this.row][this.col].x+this.p1[this.row][this.col].width/2;
                    this.cat.y=this.p1[this.row][this.col].parent.parent.parent.y+this.p1[this.row][this.col].parent.parent.y+this.p1[this.row][this.col].parent.y+this.p1[this.row][this.col].y-this.p1[this.row][this.col].height/2;
                    this.weizhucat.x=this.p1[this.row][this.col].parent.parent.parent.x+this.p1[this.row][this.col].parent.parent.x+this.p1[this.row][this.col].parent.x+this.p1[this.row][this.col-1].x+this.p1[this.row][this.col].width/2;
                    this.weizhucat.y=this.p1[this.row][this.col].parent.parent.parent.y+this.p1[this.row][this.col].parent.parent.y+this.p1[this.row][this.col].parent.y+this.p1[this.row][this.col-1].y-this.p1[this.row][this.col].height/2;
                },this);
                this.loadCats();
                
                for(let i:number=0;i<9;i++){
                    for(let j:number=0;j<9;j++){
                        this.p1[i][j].addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
                            //this.pots1[i][j].visible=!this.pots1[i][j].visible;
                            this.pots1.visibleANDtouchEnablesSet(i,j);
                            this.obstacles[i][j]=1;
                            this.fp.setcolandrow(this.row,this.col);
                            this.fp.setObstacle(this.obstacles);
                            var tmpstate:number=this.fp.moveState();
                            var flag:boolean=this.fp.getflag();
                            if(flag){
                                this.cat.visible=false;
                                this.weizhucat.visible=true;
                            }
                            if(tmpstate==-1){//状态-1代表没抓住
                                this.timer.stop();
                                console.clear();
                                this.cat.visible=false;
                                this.weizhucat.visible=false;
                                //this.removeChild(this.cat);
                                for(let ii:number=0;ii<9;ii++){
                                    for(let jj:number=0;jj<9;jj++){
                                        this.p1[ii][jj].touchEnabled=false;
                                    }
                                }
                                this.failbtn.visible=true;
                                this.pabtn.visible=true;
                            }else if(tmpstate==0 ){//状态为0代表成功围住
                                this.timer.stop();
                                console.clear();
                                
                                this.succeccText.text="用时"+(this.seconds)/10+"s";
                                this.cat.visible=false;//猫不可视
                                this.weizhucat.visible=false;
                                //this.removeChild(this.cat);
                                for(let ii:number=0;ii<9;ii++){
                                    for(let jj:number=0;jj<9;jj++){
                                        this.p1[ii][jj].touchEnabled=false;//游戏结束后点阵不可触
                                    }
                                }
                                this.succbtn.visible=true;
                                this.pabtn.visible=true;
                            }else{
                                if(tmpstate==1){//上
                                    this.row-=1;
                                }else if(tmpstate==2){//下
                                    this.row+=1;
                                }else if(tmpstate==3){//左
                                    this.col-=1;
                                }else if(tmpstate==4){//右
                                    this.col+=1;
                                }else if(tmpstate==5){
                                    if(this.row%2==0){
                                        //奇数行左上
                                        this.row-=1;
                                        this.col-=1;
                                    }else{
                                        //偶数行右上
                                        this.row-=1;
                                        this.col+=1;
                                    }
                                }else if(tmpstate==6){
                                    if(this.row%2==0){
                                        //奇数行左下
                                        this.row+=1;
                                        this.col-=1;
                                    }else{
                                        //偶数行右下
                                        this.row+=1;
                                        this.col+=1;
                                    }
                                }
                                console.log("当前神经猫的坐标："+"("+this.row+","+this.col+")");
                                //神经猫的坐标改变之后对应两只猫的图像也要随之改变
                                this.cat.x=this.p1[this.row][this.col].parent.parent.parent.x+this.p1[this.row][this.col].parent.parent.x+this.p1[this.row][this.col].parent.x+this.p1[this.row][this.col].x+this.p1[this.row][this.col].width/2;
                                this.cat.y=this.p1[this.row][this.col].parent.parent.parent.y+this.p1[this.row][this.col].parent.parent.y+this.p1[this.row][this.col].parent.y+this.p1[this.row][this.col].y-this.p1[this.row][this.col].height/2;
                                this.weizhucat.x=this.p1[this.row][this.col].parent.parent.parent.x+this.p1[this.row][this.col].parent.parent.x+this.p1[this.row][this.col].parent.x+this.p1[this.row][this.col].x+this.p1[this.row][this.col].width/2;
                                this.weizhucat.y=this.p1[this.row][this.col].parent.parent.parent.y+this.p1[this.row][this.col].parent.parent.y+this.p1[this.row][this.col].parent.y+this.p1[this.row][this.col].y-this.p1[this.row][this.col].height/2;
                            } 
                            
                           
                            
                        },this);
                    }
                }
                
                
            },100);
        }
        private loadCats():void{
            this.cat=this.catStayAndWeizhu.getStayCat();
            this.cat.visible=true;
            this.weizhucat=this.catStayAndWeizhu.getWeizhuCat();
            this.weizhucat.visible=false;
            this.cat.x=this.p1[this.row][this.col].parent.parent.parent.x+this.p1[this.row][this.col].parent.parent.x+this.p1[this.row][this.col].parent.x+this.p1[this.row][this.col].x+this.p1[this.row][this.col].width/2;
            this.cat.y=this.p1[this.row][this.col].parent.parent.parent.y+this.p1[this.row][this.col].parent.parent.y+this.p1[this.row][this.col].parent.y+this.p1[this.row][this.col].y-this.p1[this.row][this.col].height/2;
            this.weizhucat.x=this.p1[this.row][this.col].parent.parent.parent.x+this.p1[this.row][this.col].parent.parent.x+this.p1[this.row][this.col].parent.x+this.p1[this.row][this.col-1].x+this.p1[this.row][this.col].width/2;
            this.weizhucat.y=this.p1[this.row][this.col].parent.parent.parent.y+this.p1[this.row][this.col].parent.parent.y+this.p1[this.row][this.col].parent.y+this.p1[this.row][this.col-1].y-this.p1[this.row][this.col].height/2;
            this.addChildAt(this.cat,4);
            this.addChildAt(this.weizhucat,4);
            //console.log(this.cat.x+","+this.cat.y);
        }
        
        private obstacles:Array<Array<number>>;
        private init():void{
            this.succeccText.text="";
            this.succbtn.visible=false;
            this.failbtn.visible=false;
            this.pabtn.visible=false;
            this.row=4;
            this.col=4;
            //this.loadTime();
            this.timer.start();
            for(let ii:number=0;ii<9;ii++){
                for(let jj:number=0;jj<9;jj++){
                    this.p1[ii][jj].touchEnabled=true;
                    this.p1[ii][jj].visible=true;
                }
            }
            this.cat.visible=true;
            this.obstacles=new Array<Array<number>>();
            for(let i:number=0;i<9;i++){
                var tmp:Array<number>=new Array<number>();
                this.obstacles.push(tmp);
                for(let j:number=0;j<9;j++){
                    this.obstacles[i].push(0);
                }
            }
            //在中心点方圆3步内(相对于矩阵而非真实排列)随机设置五个障碍点
            for(let i:number=0;i<5;i++){
                var x:number=Math.ceil(Math.random()*3-1);
                var y:number=Math.ceil(Math.random()*3-1);
                if(x==0&&y==0){
                    i=i-1;
                }else{
                    this.obstacles[4+x][4+y]=1;//障碍点设置之后更细障碍矩阵
                    this.p1[4+x][4+y].visible=false;//被设置障碍点的位置的灰色点不可视
                }
            }
            //在图中随机选取8个位置设置障碍点
            for(let i:number=0;i<8;i++){
                var x:number=Math.ceil(Math.random()*9-1);
                var y:number=Math.ceil(Math.random()*9-1);
                if((x==4&&y==4)||this.obstacles[x][y]==1)
                {
                    i=i-1;
                }
                else{
                    this.obstacles[x][y]=1;
                    this.p1[x][y].visible=false;
                }
            }
        }
        
        private loadTime():void {
            this.seconds=0;
            this.timer.addEventListener(egret.TimerEvent.TIMER,()=>{
                this.seconds+=1;
                console.log("用时"+(this.seconds)/10+"s");
            },this);
            //this.timer.start();
        }
    }
 }

window["ziyi"]=ziyi;
window["Main"]=ziyi.Main;